<template>
  <div class="common-layout linear-gradient">
    <el-container>
      <el-header class="header-container">
        <div class="headerTitle">查询条件</div>
        <div class="header-content">
          <el-form :model="headerSearchForm" label-width="auto" class="header-search-form">
            <el-form-item label="产线">
              <el-input v-model="headerSearchForm.productionLine" placeholder="请输入产线" />
            </el-form-item>
            <el-form-item label="变量描述">
              <el-input v-model="headerSearchForm.variableDescription" placeholder="请输入变量描述" />
            </el-form-item>
            <el-form-item label="变量名">
              <el-input v-model="headerSearchForm.variableName" placeholder="请输入变量名" />
            </el-form-item>
            <el-form-item label="数据源">
              <el-select v-model="headerSearchForm.dataSource" placeholder="数据源" clearable style="width: 240px">
                <el-option v-for="item in dataSourceTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-form>
          <div class="header-buttons">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleAdd">添加</el-button>
          </div>
        </div>
      </el-header>
      <el-main>
        <div class="table-container-main">
          <el-table
            :data="tableData"
            :header-cell-style="{ background: '#eef1f6', color: '#606266', textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }"
          >
            <el-table-column prop="serialNumber" label="序号" fixed="left">
              <template #default="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="productionLine" label="产线" />
            <el-table-column prop="areaCode" label="区域代码" />
            <el-table-column prop="deviceCode" label="设备代码" />
            <el-table-column prop="variableName" label="变量名" />
            <el-table-column prop="variableAlias" label="变量别名" />
            <el-table-column prop="variableDescription" label="变量描述" />
            <el-table-column label="数据源">
              <template #default="scope">
                <span>{{ dataSourceTypeOptions[scope.row.dataSource].label }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="triggerMark" label="触发标记" />
            <el-table-column prop="variableAttribute" label="变量属性" />
            <el-table-column prop="isEnabled" label="是否启用" />
            <el-table-column prop="createTime" label="创建时间" />
            <el-table-column prop="updateTime" label="修改时间" />
            <el-table-column label="操作" fixed="right" width="240">
              <template #default="scope">
                <el-button type="warning" @click="handleEdit(scope.row)">修改</el-button>
                <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="[10, 20, 30, 50]"
            :total="pagination.total"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-main>
    </el-container>
  </div>

  <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="handleClose" ref="dialogRef" @closed="cancel">
    <el-form :model="dialogForm" label-width="auto">
      <el-form-item label="产线">
        <el-input v-model="dialogForm.productionLine" placeholder="请输入产线" />
      </el-form-item>
      <el-form-item label="区域代码">
        <el-input v-model="dialogForm.areaCode" placeholder="请输入区域代码" />
      </el-form-item>
      <el-form-item label="设备代码">
        <el-input v-model="dialogForm.deviceCode" placeholder="请输入设备代码" />
      </el-form-item>
      <el-form-item label="变量名">
        <el-input v-model="dialogForm.variableName" placeholder="请输入变量名" />
      </el-form-item>
      <el-form-item label="变量别名">
        <el-input v-model="dialogForm.variableName" placeholder="请输入变量别名" />
      </el-form-item>
      <el-form-item label="变量描述">
        <el-input v-model="dialogForm.variableDescription" placeholder="请输入变量描述" />
      </el-form-item>
      <el-form-item label="数据源">
        <el-select v-model="dialogForm.dataSource" placeholder="数据源" clearable>
          <el-option v-for="item in dataSourceTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="触发标记">
        <el-input v-model="dialogForm.triggerMark" placeholder="请输入触发标记" />
      </el-form-item>
      <el-form-item label="变量属性">
        <el-input v-model="dialogForm.variableAttribute" placeholder="请输入变量属性" />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch v-model="dialogForm.isEnabled" active-color="#13ce66" inactive-color="#ff4949" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="dialogFormSubmit"> 提交 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts">
  import { ref, reactive, computed, onMounted, onUnmounted, nextTick, defineComponent } from 'vue';
  // import { ElMessage } from 'element-plus';
  import './steelCoilServiceModelCommonStyle.less';
  import { ElLoading, ElMessage, ElMessageBox } from 'element-plus';
  interface SearchForm {
    productionLine: string;
    variableDescription: string;
    variableName: string;
    dataSource: string;
  }

  export default defineComponent({
    name: 'DataDictionary',
    setup() {
      // 表单数据
      const headerSearchForm = reactive<SearchForm>({
        productionLine: '',
        variableDescription: '',
        variableName: '',
        dataSource: '',
      });

      // 表格数据
      const tableData = ref([
        {
          productionLine: 'C622',
          areaCode: 'PL',
          deviceCode: 'trimmer',
          variableName: 'C622_WA1ZRSTW',
          variableAlias: 'c622_tcm_trimmer_width_act',
          variableDescription: '切边宽度实际值',
          dataSource: '1',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-01 08:00:00',
          updateTime: '2024-01-01 08:00:00',
        },
        {
          productionLine: 'C622',
          areaCode: 'TCM',
          deviceCode: 'ExS4',
          variableName: 'C622_DOEASC07',
          variableAlias: 'c622_tcm_shape_width',
          variableDescription: '板形仪测量带钢宽度实际值',
          dataSource: '0',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-02 09:00:00',
          updateTime: '2024-01-02 09:00:00',
        },
        {
          productionLine: 'HSM2',
          areaCode: 'PDI',
          deviceCode: 'PDI',
          variableName: 'F_CTTOLENEG',
          variableAlias: 'hsm_fx_ctc_toleneg',
          variableDescription: '热轧1580层冷下公差',
          dataSource: '2',
          triggerMark: 2,
          variableAttribute: 1,
          isEnabled: 1,
          createTime: '2024-01-03 10:00:00',
          updateTime: '2024-01-03 10:00:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntSV',
          variableName: 'C642_en_spd_set',
          variableAlias: 'c642_en_speed_set',
          variableDescription: '入口带钢速度设定值[rpm]',
          dataSource: '3',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-04 08:30:00',
          updateTime: '2024-01-04 08:30:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntPV1',
          variableName: 'C642_en_spd_act1',
          variableAlias: 'c642_en_speed_act1',
          variableDescription: '入口带钢速度实际值[MPM]',
          dataSource: '4',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-05 09:30:00',
          updateTime: '2024-01-05 09:30:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntPV2',
          variableName: 'C642_en_spd_act2',
          variableAlias: 'c642_en_speed_act2',
          variableDescription: '入口带钢速度实际值[rpm]',
          dataSource: '0',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-06 10:30:00',
          updateTime: '2024-01-06 10:30:00',
        },
        {
          productionLine: 'C622',
          areaCode: 'PL',
          deviceCode: 'trimmer',
          variableName: 'C622_WA1ZRSTW_02',
          variableAlias: 'c622_tcm_trimmer_width_act_02',
          variableDescription: '切边宽度实际值-02',
          dataSource: '1',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-07 08:00:00',
          updateTime: '2024-01-07 08:00:00',
        },
        {
          productionLine: 'C622',
          areaCode: 'TCM',
          deviceCode: 'ExS4_02',
          variableName: 'C622_DOEASC08',
          variableAlias: 'c622_tcm_shape_width_02',
          variableDescription: '板形仪测量带钢宽度实际值-02',
          dataSource: '2',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-08 09:00:00',
          updateTime: '2024-01-08 09:00:00',
        },
        {
          productionLine: 'HSM2',
          areaCode: 'PDI',
          deviceCode: 'PDI_02',
          variableName: 'F_CTTOLENEG_02',
          variableAlias: 'hsm_fx_ctc_toleneg_02',
          variableDescription: '热轧1580层冷下公差-02',
          dataSource: '3',
          triggerMark: 2,
          variableAttribute: 1,
          isEnabled: 1,
          createTime: '2024-01-09 10:00:00',
          updateTime: '2024-01-09 10:00:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntSV_02',
          variableName: 'C642_en_spd_set_02',
          variableAlias: 'c642_en_speed_set_02',
          variableDescription: '入口带钢速度设定值[rpm]-02',
          dataSource: '4',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-10 08:30:00',
          updateTime: '2024-01-10 08:30:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntPV1_02',
          variableName: 'C642_en_spd_act1_02',
          variableAlias: 'c642_en_speed_act1_02',
          variableDescription: '入口带钢速度实际值[MPM]-02',
          dataSource: '0',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-11 09:30:00',
          updateTime: '2024-01-11 09:30:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntPV2_02',
          variableName: 'C642_en_spd_act2_02',
          variableAlias: 'c642_en_speed_act2_02',
          variableDescription: '入口带钢速度实际值[rpm]-02',
          dataSource: '1',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-12 10:30:00',
          updateTime: '2024-01-12 10:30:00',
        },
        {
          productionLine: 'C622',
          areaCode: 'PL',
          deviceCode: 'trimmer_03',
          variableName: 'C622_WA1ZRSTW_03',
          variableAlias: 'c622_tcm_trimmer_width_act_03',
          variableDescription: '切边宽度实际值-03',
          dataSource: '2',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-13 08:00:00',
          updateTime: '2024-01-13 08:00:00',
        },
        {
          productionLine: 'C622',
          areaCode: 'TCM',
          deviceCode: 'ExS4_03',
          variableName: 'C622_DOEASC09',
          variableAlias: 'c622_tcm_shape_width_03',
          variableDescription: '板形仪测量带钢宽度实际值-03',
          dataSource: '3',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-14 09:00:00',
          updateTime: '2024-01-14 09:00:00',
        },
        {
          productionLine: 'HSM2',
          areaCode: 'PDI',
          deviceCode: 'PDI_03',
          variableName: 'F_CTTOLENEG_03',
          variableAlias: 'hsm_fx_ctc_toleneg_03',
          variableDescription: '热轧1580层冷下公差-03',
          dataSource: '4',
          triggerMark: 2,
          variableAttribute: 1,
          isEnabled: 1,
          createTime: '2024-01-15 10:00:00',
          updateTime: '2024-01-15 10:00:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntSV_03',
          variableName: 'C642_en_spd_set_03',
          variableAlias: 'c642_en_speed_set_03',
          variableDescription: '入口带钢速度设定值[rpm]-03',
          dataSource: '0',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-16 08:30:00',
          updateTime: '2024-01-16 08:30:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntPV1_03',
          variableName: 'C642_en_spd_act1_03',
          variableAlias: 'c642_en_speed_act1_03',
          variableDescription: '入口带钢速度实际值[MPM]-03',
          dataSource: '1',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-17 09:30:00',
          updateTime: '2024-01-17 09:30:00',
        },
        {
          productionLine: 'C642',
          areaCode: 'EN',
          deviceCode: 'C642_SpdEntPV2_03',
          variableName: 'C642_en_spd_act2_03',
          variableAlias: 'c642_en_speed_act2_03',
          variableDescription: '入口带钢速度实际值[rpm]-03',
          dataSource: '2',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-18 10:30:00',
          updateTime: '2024-01-18 10:30:00',
        },
        {
          productionLine: 'C622',
          areaCode: 'PL',
          deviceCode: 'trimmer_04',
          variableName: 'C622_WA1ZRSTW_04',
          variableAlias: 'c622_tcm_trimmer_width_act_04',
          variableDescription: '切边宽度实际值-04',
          dataSource: '3',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-19 08:00:00',
          updateTime: '2024-01-19 08:00:00',
        },
        {
          productionLine: 'C622',
          areaCode: 'TCM',
          deviceCode: 'ExS4_04',
          variableName: 'C622_DOEASC10',
          variableAlias: 'c622_tcm_shape_width_04',
          variableDescription: '板形仪测量带钢宽度实际值-04',
          dataSource: '4',
          triggerMark: 3,
          variableAttribute: 2,
          isEnabled: 1,
          createTime: '2024-01-20 09:00:00',
          updateTime: '2024-01-20 09:00:00',
        },
        {
          productionLine: 'HSM2',
          areaCode: 'PDI',
          deviceCode: 'PDI_04',
          variableName: 'F_CTTOLENEG_04',
          variableAlias: 'hsm_fx_ctc_toleneg_04',
          variableDescription: '热轧1580层冷下公差-04',
          dataSource: '0',
          triggerMark: 2,
          variableAttribute: 1,
          isEnabled: 1,
          createTime: '2024-01-21 10:00:00',
          updateTime: '2024-01-21 10:00:00',
        },
      ]);

      // const dataSourceType = ref('');
      let dialogVisible = ref(false);
      let dialogTitle = ref('');
      const dialogForm = reactive({
        productionLine: '',
        areaCode: '',
        deviceCode: '',
        variableName: '',
        variableAlias: '',
        variableDescription: '',
        // dataSourceType: '',
        dataSource: '',
        triggerMark: '',
        variableAttribute: '',
        isEnabled: false,
      });
      const dialogRef = ref();

      const dataSourceTypeOptions = ref([
        { label: 'PLC', value: '0' },
        { label: '传感器', value: '1' },
        { label: 'AI算法', value: '2' },
        { label: '称重传感器', value: '3' },
        { label: '电池管理系统', value: '4' },
      ]);

      // 分页数据
      const pagination = reactive({
        currentPage: 1,
        pageSize: 10,
        total: tableData.value.length,
      });

      // 窗口大小变化处理
      const handleResize = () => {
        nextTick(() => {
          // 这里可以添加表格自适应高度的逻辑
        });
      };

      // 获取表格数据
      const fetchTableData = async () => {
        try {
          // 这里添加获取数据的API调用
          // const res = await api.getTableData({ ...headerSearchForm, ...pagination });
          // tableData.value = res.data;
          // pagination.total = res.total;
        } catch (error) {
          ElMessage.error('获取数据失败');
        }
      };

      // 查询方法
      const handleSearch = () => {
        pagination.currentPage = 1;
        fetchTableData();
      };

      // 重置方法
      const handleReset = () => {
        (Object.keys(headerSearchForm) as Array<keyof SearchForm>).forEach((key) => {
          headerSearchForm[key] = '';
        });
        handleSearch();
      };

      // 添加方法
      const handleAdd = () => {
        // 这里添加新增逻辑
        console.log('添加');
        dialogVisible.value = true;
        dialogTitle.value = '新增';
        Object.assign(dialogForm, {
          productionLine: '',
          areaCode: '',
          deviceCode: '',
          variableName: '',
          variableAlias: '',
          variableDescription: '',
          // dataSourceType: '',
          dataSource: '',
          triggerMark: '',
          variableAttribute: '',
          isEnabled: false,
        });
      };

      // 编辑方法
      const handleEdit = (row: any) => {
        // 这里添加编辑逻辑
        dialogVisible.value = true;
        dialogTitle.value = '修改';
        Object.assign(dialogForm, { ...row });
        dialogForm.dataSource = dataSourceTypeOptions.value[Number(row.dataSource)].label;
      };

      const cancel = () => {
        dialogVisible.value = false;
      };

      const dialogFormSubmit = () => {
        // 这里添加表单提交逻辑
        console.log('表单提交', dialogForm);
        dialogVisible.value = false;
        fetchTableData();
      };

      // 删除方法
      const handleDelete = (row: any) => {
        // 这里添加删除逻辑
        console.log('删除', row);
      };

      // 分页大小变化
      const handleSizeChange = (val: number) => {
        pagination.pageSize = val;
        fetchTableData();
      };

      // 页码变化
      const handleCurrentChange = (val: number) => {
        pagination.currentPage = val;
        fetchTableData();
      };

      const handleClose = (done: () => void) => {
        ElMessageBox.confirm('Are you sure to close this dialog?')
          .then(() => {
            done();
          })
          .catch(() => {
            // catch error
          });
      };

      // 生命周期钩子
      onMounted(() => {
        // fetchTableData();
        window.addEventListener('resize', handleResize);
      });

      onUnmounted(() => {
        window.removeEventListener('resize', handleResize);
      });

      return {
        headerSearchForm,
        tableData,
        pagination,
        handleSearch,
        handleReset,
        handleAdd,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        dataSourceTypeOptions,
        // dataSourceType,
        dialogVisible,
        handleClose,
        dialogTitle,
        dialogForm,
        dialogRef,
        cancel,
        dialogFormSubmit,
      };
    },
  });
</script>
